<?php

namespace backend\models\form;

use common\helpers\Util;
use common\libs\Constants;
use Yii;

/**
 * This is the model class for table "sports_brand".
 *
 * @property int $brand_id
 * @property string $brand_name
 * @property string $brand_logo
 * @property string $brand_msg
 * @property string $brand_manager
 * @property string $brand_account
 * @property string $created_at
 * @property string $updated_at
 */
class BrandForm extends \yii\db\ActiveRecord
{

    /** @var string $advertisement store file path */
    private $advertisementFilePath = '@uploads/setting/brand/';

    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return 'sports_brand';
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
          [['brand_name', 'brand_logo', 'brand_msg', 'brand_manager', 'brand_account', 'created_at', 'updated_at'], 'required'],
          [['brand_logo'], 'string'],
          [['created_at', 'updated_at'], 'safe'],
          [['brand_name', 'brand_manager', 'brand_account'], 'string', 'max' => 45],
          [['brand_msg'], 'string', 'max' => 255],
          [['brand_account'], 'unique'],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return
              [
                  'brand_id'      => Yii::t('app', 'Brand ID'),
                  'brand_name'    => Yii::t('app', 'Brand Name'),
                  'brand_logo'    => Yii::t('app', 'Brand Logo'),
                  'brand_msg'     => Yii::t('app', 'Brand Msg'),
                  'brand_manager' => Yii::t('app', 'Brand Manager'),
                  'brand_account' => Yii::t('app', 'Brand Account'),
                  'created_at'    => Yii::t('app', 'Created At'),
                  'updated_at'    => Yii::t('app', 'Updated At'),
              ];
    }

    public function beforeSave($insert)
    {
        var_dump(222222);die;
        Util::handleModelSingleFileUploadAbnormal($this, 'brand_logo', $this->advertisementFilePath, $this->getOldAttribute('brand_logo'));
        $value = [
          'brand_logo' => $this->brand_logo,
          'created_at' => $this->getIsNewRecord() ? time() : $this->created_at,
          'updated_at' => time(),
        ];
        $this->value = json_encode($value);
        return parent::beforeSave($insert);
    }
}
